package com.netistate.dao;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.netistate.base.BaseDao;
import com.netistate.pojo.Project;

@Repository
public class ProjectDao extends BaseDao<Project, Integer>{

	/**
	 * 根据条件来查找项目
	 * @param projectManager
	 * @param starttime
	 * @param endtime
	 * @param projectProcess
	 * @return projectList
	 */
	@SuppressWarnings("unchecked")
	public List<Project> queryProjectByCondition(String projectManager,
			String starttime, String endtime, String projectProcess) {
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		starttime = starttime+" 24:00:00";
		
		if("现在".equals(endtime)){
	    	  Date date = new Date();
	    	  endtime = df.format(date);
	    }
		endtime = endtime+" 24:00:00";
	    String sql = "from Project where createtime>='"+starttime+"' and createtime<='"+endtime+"'";
	    if("所有".equals(projectManager)){
	    	if(!"所有".equals(projectProcess)){
	    		sql = sql +" and projectProcess='"+projectProcess+"'";
	    	}
	    }else{
	    	sql = sql +" and projectManager='"+projectManager+"'";
	    	if(!"所有".equals(projectProcess)){
	    		sql = sql +" and projectProcess='"+projectProcess+"'";
	    	}
	    }
	    Query query = getSession().createQuery(sql);
		return query.list();
	}

	/**
	 * 查找所有项目按项目首字母排序
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Project> findAllProject() {
		String hql = "from Project order by projectName asc";
		Query query = getSession().createQuery(hql);
		return query.list();
	}

	/**
	 * 过滤查询projectList,条件为阶段为售前
	 * @param sq
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Project> findProjectByProjectProcess(String sq) {
		String hql = "from Project where projectProcess = ? order by projectName asc";
		Query query = getSession().createQuery(hql);
		query.setString(0, sq);
		
		return query.list();
	}

	/**
	 * 登录后默认查找一个月内的项目信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Project> findProjectListInOneMonth() {
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    calendar.add(Calendar.DATE,-30);
	    Date date2 = calendar.getTime();
		
		String endtime = df.format(date) + " 24:00:00";
		String begintime = df.format(date2) + " 24:00:00";
		
		String hql = "from Project where createtime >= '"+begintime+"' and createtime <='"+endtime+"'";
		Query query = getSession().createQuery(hql);
		
		return query.list();
	}


}
